function toAddProductCategory() {
	$.ajax({
		url:"productCategoryServlet",
		method:"post",
		data:{action:"toAddProductCategory"},
		success:function(data){
			$("#addProductCategory").html(data);
		}
	},"json");
}

function selectProductCategoryLevel(obj) {
	var type=$(obj).val();
	if (type==1) {
		$("#productCategoryLevel1").parent().parent().hide();
		$("#productCategoryLevel2").parent().parent().hide();
		
		$("#productCategoryLevel1").html(" <option value='0' selected='selected'>请选择...</option>");
		$("#productCategoryLevel2").html(" <option value='0' selected='selected'>请选择...</option>");
	}
	if (type==2) {
		$("#productCategoryLevel1").parent().parent().show();
		$("#productCategoryLevel2").parent().parent().hide();
		
		$("#productCategoryLevel2").html(" <option value='0' selected='selected'>请选择...</option>");
		queryProductCategoryList(0,'productCategoryLevel1');
	}
	if (type==3) {
		$("#productCategoryLevel1").parent().parent().show();
		$("#productCategoryLevel2").parent().parent().show();
		queryProductCategoryList(0,'productCategoryLevel1');
		$("#productCategoryLevel2").html(" <option value='0' selected='selected'>请选择...</option>");
	}
}

function queryProductCategoryList(parentId,selectId) {
	$("#"+selectId).html("<option value='0' selected='selected'>请选择...</option>");
	$.ajax({
		url:"productCategoryServlet",
		method:"post",
		data:{action:"queryProductCategoryList",
			parentId:parentId},
		dataType:"json",
		success:function(data){
			for (var i = 0; i < data.length; i++) {
				$("#"+selectId).append("<option value='"+data[i].id+"'>"+data[i].name+"</option>");
				
			}
			
			
		}
	});
	
}


function levelChange(obj) {
	$("#productCategoryLevel2").html(" <option value='0' selected='selected'>请选择...</option>");
	var type=$("#type").val();
	if (type==3) {
		var temp=$(obj).val();
		if (temp!=0) {
			queryProductCategoryList(temp,'productCategoryLevel2');
		}else{
			$("#productCategoryLevel2").html(" <option value='0' selected='selected'>请选择...</option>");
		}
		
	}
}

function saveOrUpdate() {
	var id=$("#id").val();
	//新增
	var type=$("#type").val();
	if (type!=0) {
		var name=$("#name").val();
		var parentId="";
		
		if (type==3) {
			parentId=$("#productCategoryLevel2").val();
		}
		if (type==2) {
			parentId=$("#productCategoryLevel1").val();
		}
		if (type==1) {
			parentId=0;
		}
		$.ajax({
			url:"productCategoryServlet",
			method:"post",
			data:{action:"saveOrUpdate",
				parentId:parentId,
				name:name,
				type:type},
			dataType:"json",
			success:function(data){
				if (data==1) {
				location.reload();	
				}
				if (id!=null || id!="") {
					//修改
					modifyProductCategory();
				}
				
			}
		});
	}else{
		showMessage("请选择分类!");
	}
	
}

function toUpdateProductCategoryList(obj) {
	var id=$(obj).val();
	$.ajax({
		url:"productCategoryServlet",
		method:"post",
		data:{action:"toUpdateProductCategoryList",
			id:id},
		success:function(data){
			$("#addProductCategory").html(data);
		}
	});
}
function modifyProductCategory() {
	var id=$("#id").val();
	var name=$("#name").val();
	var type=$("#type").val();
	var productCategoryLevel1=$("#productCategoryLevel1").val();
	var productCategoryLevel2=$("#productCategoryLevel2").val();
	$.ajax({
		url:"productCategoryServlet",
		method:"post",
		data:{action:"modifyProductCategory",
			id:id,
			name:name,
			type:type,
			productCategoryLevel1:(productCategoryLevel1=="" || productCategoryLevel1==null)?0:productCategoryLevel1,
			productCategoryLevel2:(productCategoryLevel2=="" || productCategoryLevel2==null)?0:productCategoryLevel2},
		dataType:"json",
		success:function(data){
			if (data.status==1) {
			location.reload();	
			}else{
				showMessage(data.message);
			}
		}
	});
	
}
function checkProduct() {
	var productCategoryLevel1=$("#productCategoryLevel1").val();
	var productCategoryLevel2=$("#productCategoryLevel2").val();
	var productCategoryLevel3=$("#productCategoryLevel3").val();
	var name=$("#name").val();
	var photo=$("#photo").val();
	var price=$("#price").val();
	var stock=$("#stock").val();
	var description=$("#description").val();
	if (productCategoryLevel1==0 || productCategoryLevel2==0 || productCategoryLevel3==0) {
		layer.msg('一、二、三级分类不能为空', {
  		  time: 20000, //20s后自动关闭
  		  btn: ['确定']
  		});
		return false;
	}
	if (name=="" || name==null) {
		layer.msg('商品名称不能为空', {
	  		  time: 20000, //20s后自动关闭
	  		  btn: ['确定']
	  		});
		return false;
	}
	
	if (price=="" || price==null) {
		layer.msg('商品单价不能为空', {
	  		  time: 20000, //20s后自动关闭
	  		  btn: ['确定']
	  		});
		return false;
	}
	if (stock=="" || stock==null) {
		layer.msg('商品库存不能为空', {
	  		  time: 20000, //20s后自动关闭
	  		  btn: ['确定']
	  		});
		return false;
	}
	return true;
	
}

function updateProduct() {
	var fd = new FormData(document.getElementById("productAdd"));
	$.ajax({
		url:"AdminProductServlet?action=updateProduct",
		type: "POST",
		data: fd,
		dataType: "json",
		processData: false,  // 不处理数据
		contentType: false,   // 不设置内容类型
		success:function(data){
			  if (data.count>0) {
		            window.location.href ="AdminProductServlet?action=index&currpage=1";
			  }else{
				  layer.msg('修改失败', {
			  		  time: 20000, //20s后自动关闭
			  		  btn: ['确定']
			  		});
			  }
		},
		error:function(e){
		    console.log(e);
		    showMessage("错误。。。");
		}
	});
	
}	

//商品删除
function deleteById(id) {
	if (confirm("确认删除该商品吗?")) {
		$.getJSON("AdminProductServlet?action=deleteById&id="+id,function(data){
			if (data>0) {
				showMessage("删除成功");
				window.location.href ="AdminProductServlet?action=index&currpage=1";
			}else{
				showMessage("删除失败");
			}
		})
	}else{
		showMessage("您取消了删除");
	}
}

//分类删除
function deleteProductCategory(id,type) {
	if (confirm("确认删除该分类吗?")) {
		$.getJSON("productCategoryServlet?action=deleteProductCategory&id="+id+"&type="+type,function(data){
			if (data>0) {
				showMessage("删除成功");
				window.location.href ="productCategoryServlet?action=index&currpage=1";
			}else{
				showMessage("该分类有商品不能删除");
			}
		})
	}else{
		showMessage("您取消了删除");
	}
}


//修改用户
function updateUser() {
	var loginName=$("#loginName").val();
	var userName=$("#userName").val();
	var sex=$("#sex").val();
	var identityCode=$("#identityCode").val();
	var email=$("#email").val();
	var mobile=$("#mobile").val();
	var type=$("#type").val();
	var id=$("#id").val();
	
	$.ajax({
		url:"AdminUserServlet",
		method:"post",
		data:{action:"updateUser",
			loginName:loginName,
			userName:userName,
			sex:sex,
			identityCode:identityCode,
			email:email,
			mobile:mobile,
			type:type,
			id:id},
		success:function(data){
			if (data>0) {
				showMessage("修改成功");
				location.href="AdminUserServlet?action=queryUserList&currpage=1";
			}else{
				showMessage("修改失败");
			}
		}
	});
}

//添加用户
function addUser() {
	var loginName=$("#loginName").val();
	var pwd=$("#pwd")
	var userName=$("#userName").val();
	var sex=$("#sex").val();
	var identityCode=$("#identityCode").val();
	var email=$("#email").val();
	var mobile=$("#mobile").val();
	var type=$("#type").val();
	var id=$("#id").val();
	
	$.ajax({
		url:"AdminUserServlet",
		method:"post",
		data:{action:"AddUser",
			loginName:loginName,
			userName:userName,
			sex:sex,
			identityCode:identityCode,
			email:email,
			mobile:mobile,
			type:type,
			pwd:pwd},
		success:function(data){
			if (data>0) {
				showMessage("添加成功");
				location.href="AdminUserServlet?action=queryUserList&currpage=1";
			}else{
				showMessage("添加失败");
			}
		}
	});
}